var app = new Vue({
	el: '#app',
	data: {
		panel: 'medicine',
		menu:'medicine',
		isLogin: false,
		account: '',
		password: '',
		accountName: '',
		type: 'clerk',
		oldPassword: '',
		newPassword: '',
		viewBillMsg: {},
		viewMedicineMsg: {},
		medicine: [],
		medicineShow: [],
		medicinePage: 1,
		billShow: null,
		billPage: 1,
		bill: null,
		operateMedicineMsg: {},
		operateType: "purchase",
		operateAmout: null,
		operateMessage: '',
		operateRemark: ''
	},
	watch: {
		medicinePage: function(){
			this.renderMedicine();
		},
		billPage: function(){
			this.renderBill();
		}
	},
	methods: {
		getLoginStatus: function(){
			var self = this;
			$.get('/api/isLogin',function(data,status){
				if (data.success == "ok") {
					self.isLogin = true;
					self.accountName = data.result.name;
				} else {
					self.isLogin = false;
					self.accountName = "";
					window.location.href = "/login.html"
				}
			})
		},
		login: function(){
			var self = this;
			$.post("/api/login",
			{
				account: this.account,
				password: this.password,
				type: this.type
			},
			function(data,status){
				if (data.success == "ok") {
					self.getLoginStatus();
				}
			});
		},
		logout: function(){
			var self = this;
			$.post("/api/logout",{},function(data,status){
				if (data.success = "ok") {
					self.getLoginStatus();
				}
			});
		},
		changePassword: function(){
			var self = this;
			$.post("/api/changePassword",
			{
				oldPassword: self.oldPassword,
				newPassword: self.newPassword
			},function(data,status){
				if (data.success == "ok") {
					self.oldPassword = self.newPassword = null;
					$('#passwordModal').modal('hide');
					swal("修改成功", "密码已更新", "success");
				} else {
					swal("修改失败", data.msg, "error");
				}
			});
		},
		changeMenu: function(param){
			this.menu = param;
			this.panel = param;
		},
		getMedicine: function(){
			var self = this;
			$.post("/api/viewMedicine",{},function(data, status){
				if (data.success == "ok") {
					self.medicine = data.result.data;
					self.renderMedicine();
				}
			})
		},
		renderMedicine: function(){
			var self = this;
			var page = self.medicinePage;
			self.medicineShow = [];
			for (var i = 0; i < 20 ; i++) {
				if (self.medicine[(page-1)*20+i] != undefined) {
					self.medicineShow.push(self.medicine[(page-1)*20+i]);
				}
			}
		},
		renderBill: function(){
			var self = this;
			var page = self.billPage;
			self.billShow = [];
			for (var i = 0; i < 20 ; i++) {
				if (self.bill[(page-1)*20+i] != undefined) {
					self.billShow.push(self.bill[(page-1)*20+i]);
				}
			}
		},
		getBill: function(){
			var self = this;
			$.post("/api/viewBill",{},function(data, status){
				if (data.success == "ok") {
					self.bill = data.result.data;
					self.renderBill();
				}
			})
		},
		changePage: function(type, page){
			this[type+"Page"] = page;
		},
		logout: function(){
			$.post("/api/logout",{},function(data, status){
				if (data.success == "ok") {
					window.location.href = "/login.html"
				} else {
					alert("退出错误");
				}
			})
		},
		viewMedicine: function(msg){
			console.log(msg)
			this.viewMedicineMsg = msg;
			$('#medicineModal').modal('show')
		},
		viewBill: function(msg){
			console.log(msg);
			this.viewBillMsg = msg;
			$('#billModal').modal('show')
		},
		operateMedicine: function(msg){
			console.log(msg);
			this.operateMedicineMsg = msg;
			$('#operateModal').modal('show');
		},
		showChangePassword: function(){
			$('#passwordModal').modal('show');
		},
		operate: function(){
			var self = this;
			var medicine_id = this.operateMedicineMsg.id;
			var type = this.operateType;
			var amout = this.operateAmout;
			var message = this.operateMessage;
			var remarks = this.operateRemark;
			if (this.operateMedicineMsg.id == undefined) {
				swal("保存失败", "id信息缺失", "error")
			} else if ( this.operateType == undefined) {
				swal("保存失败", "类型缺失", "error")
			} else if ( this.operateAmout == undefined) {
				swal("保存失败", "数目不能为空", "error")
			} else {
				$.post("/api/addBill",{
					medicine_id:medicine_id,
					type: type,
					amout:amout,
					message:message,
					remarks:remarks
				},function(data, status){
					if (data.success == "ok") {
						self.operateAmout = self.operateMessage = self.operateRemark = null;
						$('#operateModal').modal('hide');
						swal("保存成功", "数据已插入", "success");
					} else {
						swal("保存失败", data.msg, "error");
					}
				})
			}
		}
	},
	mounted: function(){
		this.getLoginStatus();
		this.getMedicine();
		this.getBill();
	}
})
